<?php
require_once '../../classes/includeAll_Layer2.php';
//session_start(); //debug

$flag = 0;
$query = "";
$saveID = false;
$queries = new mysqlqueries();  
require '../../resources/getConnection.php';

if(!isset($_POST["prog"]) || !isset($_SESSION["startDate"]) || !isset($_SESSION["SCPResult"]) || $_SESSION["SCPResult"]==false)
{
  echo "error";
  exit();
}

$flagFilter = "";
switch ($_POST["prog"]) {
  case 1:
    $flagFilter = "DHCP";
    break;  
  case 2:
    $flagFilter = "Switch";
    break;  
  case 3:
    $flagFilter = "Liste";
    break;  
  case 4:
    $flagFilter = "Geraete";
    break;
  case 5:
    $flagFilter = "Datenbank";
    break;

}

if(!isset($_SESSION["flagID"]) || $_SESSION["flagID"] == -1 || !isset($_SESSION["flagIDGPF"]) || $_SESSION["flagIDGPF"] == -1)
{
  $startDate=$_SESSION["startDate"];

  $query = $queries->AddLimit($queries->ExtendOrder($queries->ExtendWhere($queries->GetTable($queries->Flag, null), array("name","date"), array("LIKE",">="), array("FullScanProgress",$startDate)),"date DESC"), 0, 1);
  $queryGPF = $queries->AddLimit($queries->ExtendOrder($queries->ExtendWhere($queries->GetTable($queries->Flag, null), array("name","date"), array("NOT LIKE",">="), array("FullScanProgress",$startDate)),"date DESC"), 0, 1);
  $saveID = true;
}
else
{
  $query = $queries->GetIndex($queries->Flag, $_SESSION["flagID"], null, null);
  $queryGPF = $queries->GetIndex($queries->Flag, $_SESSION["flagIDGPF"], null, null);
}

$result = $connection->query($query);
if($row =  mysqli_fetch_array($result))
{
  $flag = $row["flag"];
  if($saveID)
    $_SESSION["flagID"]= $row["ID"];
}

$result = $connection->query($queryGPF);
if($row =  mysqli_fetch_array($result))
{
  $flagGPF = $row["flag"];
  if($saveID)
    $_SESSION["flagIDGPF"]= $row["ID"];
}


if($flag != 0)
{
  $notice = "";
  foreach ((new allFlags())->FullscanProgressFlags as $key => $value) {
    if(($flag & $value)>0)
    {
      if(strpos($key, $flagFilter)!==false)
      {
        $notice.=$key.' ';
      }
    }
  }
}

if($notice != "")
{
  if(strpos($notice, "Fehler")!==false)
    echo 'error';
  else if(strpos($notice, "Warnung")!==false)
    echo 'warning';
  else
    echo 'true';
  exit();
}
  
if($flagGPF != 0)
{
  $notice = "";
  foreach ((new allFlags())->GlobalFlags as $key => $value) {
    if(($flagGPF & $value)>0)
    {
      $notice.=$key.' ';
    }
  }
  //echo $notice;
  if(strpos($notice, "Fehler")!==false)
  {
    echo 'error';
    exit();
  }
}
else
{
  echo "false";
  exit();
}
